package com.hdu.careerplan.controller;

import com.alibaba.fastjson.JSONArray;
import com.hdu.careerplan.enums.StatusCode;
import com.hdu.careerplan.exception.WrongKeyInJwtException;
import com.hdu.careerplan.pojo.dto.CityJobChoiceListDTO;
import com.hdu.careerplan.pojo.entity.CompetenceInfo;
import com.hdu.careerplan.pojo.pythonData.CompanyInfoData;
import com.hdu.careerplan.pojo.pythonData.CompanyListData;
import com.hdu.careerplan.pojo.vo.Result;
import com.hdu.careerplan.pojo.vo.ResultData;
import com.hdu.careerplan.service.RecommendService;
import com.hdu.careerplan.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/recommend")
public class RecommendController {

    private final RecommendService recommendService;

    @Autowired
    public RecommendController(RecommendService recommendService) {
        this.recommendService = recommendService;
    }

    // top10相关企业，一个城市多个岗位
    @GetMapping({"company/initial_choice"})
    public ResultData companyInitialJob(CityJobChoiceListDTO cityJobChoiceListDTO, ServletResponse servletResponse) throws IOException, InterruptedException {
        List<CompanyListData> companyListDataList = recommendService.relateCompany(0, cityJobChoiceListDTO);
        if (companyListDataList == null) { // 空数据的返回情况
            return Result.noContent(null);
        }
        return Result.success(companyListDataList.get(0));
    }


    // top10相关企业，一个城市多个岗位
    @GetMapping({"company/relate_job"})
    public ResultData companyRelatedJob(CityJobChoiceListDTO cityJobChoiceListDTO) throws IOException, InterruptedException {
        List<CompanyListData> companyListDataList = recommendService.relateCompany(0, cityJobChoiceListDTO);
        if (companyListDataList.isEmpty()) { // 空数据的返回情况
            return Result.noContent(null);
        }
        return Result.success(companyListDataList);
    }

    // top10相关企业，一个岗位多个城市
    @GetMapping("company/relate_region")
    public ResultData companyRelatedRegion(CityJobChoiceListDTO cityJobChoiceListDTO) throws IOException, InterruptedException {
        List<CompanyListData> companyListDataList = recommendService.relateCompany(1, cityJobChoiceListDTO);
        if (companyListDataList.isEmpty()) { // 空数据的返回情况
            return Result.noContent(null);
        }
        return Result.success(companyListDataList);
    }

    /**
     * 企业具体相关信息
     */
    @GetMapping("company/info")
    public ResultData getCompanyInfo(String company) throws IOException, InterruptedException {
        CompanyInfoData companyInfo = recommendService.getCompanyInfo(company);
        if (companyInfo == null) {
            return Result.responseWithNonData(StatusCode.NO_CONTENT);
        }
        return Result.responseWithData(StatusCode.SUCCESS, companyInfo);
    }

    /**
     * 根据简历获取推荐信息（没有方向的时候）
     * @param servletRequest
     * @return
     * @throws IOException
     * @throws InterruptedException
     */
    @GetMapping("position/ambiguity")
    public ResultData getPositionRecommendationFromResume(ServletRequest servletRequest) throws IOException, InterruptedException, WrongKeyInJwtException {
        Object userId = JwtUtil.getInfoFromJWT(servletRequest, "userId");
        JSONArray positionRecommendationFromResume = recommendService.getPositionRecommendationFromResume(Long.valueOf(userId.toString()));
        if (positionRecommendationFromResume == null || positionRecommendationFromResume.isEmpty()) {
            return Result.responseWithNonData(StatusCode.NO_CONTENT);
        }
        else return Result.responseWithData(StatusCode.SUCCESS, positionRecommendationFromResume);
    }

    @GetMapping("/position/match")
    public ResultData getCompetenceScore(ServletRequest servletRequest) throws IOException, InterruptedException, WrongKeyInJwtException {
        Object userId = JwtUtil.getInfoFromJWT(servletRequest, "userId");
        List<CompetenceInfo> competenceScore = recommendService.getCompetenceScore(Long.valueOf(userId.toString()));
        if (competenceScore == null || competenceScore.isEmpty()) {
            return Result.responseWithNonData(StatusCode.NO_CONTENT);
        }
        else return Result.responseWithData(StatusCode.SUCCESS, competenceScore);

    }



    @GetMapping("interest")
    public ResultData getDirectionRecommendationFromInterest(ServletRequest servletRequest) throws IOException, InterruptedException, WrongKeyInJwtException {
        Object userId = JwtUtil.getInfoFromJWT(servletRequest, "userId");
        JSONArray directionFromInterestScore = recommendService.getDirectionFromInterestScore(Long.valueOf(userId.toString()));
        if (directionFromInterestScore == null || directionFromInterestScore.isEmpty()) {
            return Result.responseWithNonData(StatusCode.NO_CONTENT);
        }
        else return Result.responseWithData(StatusCode.SUCCESS, directionFromInterestScore);
    }






}
